;(function(factory) {
	if(typeof define === 'function' && define.amd) {
		define(['frame/XFrame_Graph', 'frame/BaseShape'], factory);
	}else {
		factory(XFrame_Graph, BaseShape, window);
	}
}(function(XFrame_Graph, BaseShape, global) {
	var CommonShape = {};
	
	CommonShape.ImageShape = BaseShape.extend({
		init : function(conf){
			
			this.url = conf.url;
			this.offsetInfo = conf.offsetInfo;
			this.scaleSize = conf.scaleSize;
			XFrame_Graph.registImage(this.url);
			this.disableEvents = true;
			this.unbindEvent();
		},
		drawDefine : function(){
			var dd = new XFrame_Graph.drawDefiner();
			if(this.offsetInfo && this.scaleSize){
				dd.draw('Image', this.url, this.offsetInfo[0], this.offsetInfo[1], this.offsetInfo[2], this.offsetInfo[3], this.posX, this.posY, this.scaleSize[0], this.scaleSize[1]);
			}else{
				dd.draw('Image', this.url, this.posX, this.posY);
			}
			return dd.getCommands();
		}
	});
	
	CommonShape.TextShape = BaseShape.extend({
		init : function(conf){
			this.text = conf.text || 'TextShape';
			this.font = conf.font || '14px \'Microsoft YaHei\',Arail';
			this.textAlign = conf.align || 'left';
			this.textBaseLine = conf.baseline || 'top';
			this.textColor = conf.color || 'black';
			this.nofill = !!conf.nofill;
			
			this.disableEvents = true;
			this.unbindEvent();
		},
		drawDefine : function(){
			var dd = new XFrame_Graph.drawDefiner();
			dd.style('Stroke', this.textColor);
			dd.style('Fill', this.textColor);
			dd.style('Text', this.font, this.textAlign, this.textBaseLine);
			
			dd.draw('Text', this.text, this.posX, this.posY, this.width, !this.nofill);
			return dd.getCommands();
		}
	});
	
	CommonShape.processShape = BaseShape.extend({
		init : function(conf){
			this.processValue = conf.pv || 0;
			this.borderColor = conf.borderColor || '#ccc';
			this.bgColor = conf.bgColor || 'white';
			this.processColor = conf.processColor || '#8ED3FF';
			
			this.isShowText = !!conf.showText;
			this.isAutoText = !conf.text;
			this.text = conf.text || 'auto';
			this.textColor = conf.textColor || 'black';
			this.textPos = conf.textPos || 'inner';
			this.textFont = conf.textFont || '10px \'Microsoft YaHei\',Arail';
			
			this.disableEvents = true;
			this.unbindEvent();
		},
		drawDefine : function(){
			var dd = new XFrame_Graph.drawDefiner();
			dd.style('Stroke', this.borderColor);
			if($.isNumeric(this.processValue)){
				if(this.processValue < 0){
					this.processValue = 0;
				}else if(this.processValue > 1){
					this.processValue = 1;
				}else{
					this.processValue = +this.processValue;
				}
			}else{
				this.processValue = 0;
			}
			if(this.processValue === 1){
				dd.style('Fill', this.processColor);
			}else if(this.processValue === 0){
				dd.style('Fill', this.bgColor);
			}else{
				dd.style('Fill', {
					creator : [this.posX, 0 ,this.posX + this.width, 0],
					stops : [[0, this.processColor], [this.processValue, this.processColor], [this.processValue, this.bgColor], [1, this.bgColor]]
				});
			}
			
			dd.draw('FilletRect', this.posX, this.posY, this.width, this.height, this.height/2);
			if(this.isShowText){
				
				if(this.isAutoText){
					this.text = ((this.processValue * 1000) | 0) / 10 + '%';
				}
				if(this.textPos === 'inner'){
					dd.style('Stroke', this.textColor);
					dd.style('Fill', this.textColor);
					dd.style('Text', this.textFont, 'center', 'middle');
					dd.draw('Text', this.text, this.posX + (this.width / 2), this.posY+ (this.height / 2), this.width, true);
				}
			}
			
			return dd.getCommands();
		},
		setProcessValue : function(pv){
		    if(!$.isNumeric(pv) ){
		        return;
		    }
		    pv = +pv;
		    if(pv > 1 || pv < 0){
		        this.processValue = pv;
		        this.updateCommands();
		        this.refresh();
		    }
		}
	});
	
	CommonShape.ButtonShape = BaseShape.extend({
	    init : function(conf){
	        conf = conf || {};
	        this.text = conf.text;
	        this.width = conf.width || 100;
	        this.height = conf.height || 15;
	        this.borderColor = '#888';
            this.textColor = conf.textColor || '#757575';
            this.textPos = conf.textPos || 'inner';
            this.textFont = conf.textFont || '12px \'Microsoft YaHei\',Arail';
            this.dragable = false;
            this.canActive = false;
            this.canInActive = false;
	    },
	    drawDefine : function(){
	        var dd = new XFrame_Graph.drawDefiner();
	        dd.style('Stroke', this.borderColor);
	        dd.style('Fill', {
                    creator : [0, this.posY , 0, this.posY + this.height],
                    stops : [[0, '#fff'], [0.25, '#fff'], [0.25, '#fff'], [1, '#efefef']]
                });      
            dd.draw('FilletRect', this.posX, this.posY, this.width, this.height, 3);           
            dd.style('Stroke', this.textColor);
            dd.style('Fill', this.textColor);
            dd.style('Text', this.textFont, 'center', 'middle');
            dd.draw('Text', this.text, this.posX + (this.width / 2), this.posY+ (this.height / 2), this.width, true);
            
            
            dd.changeEditType('active');
            dd.style('Stroke', this.borderColor);
            dd.style('Fill', {
                    creator : [0, this.posY , 0, this.posY + this.height],
                    stops : [[0, '#858585'],  [1, '#c7c7c7']]
                });
            dd.draw('FilletRect', this.posX, this.posY, this.width, this.height, 3);
            dd.style('Stroke', this.textColor);
            dd.style('Fill', '#353535');
            dd.style('Text', this.textFont, 'center', 'middle');
            dd.draw('Text', this.text, this.posX + (this.width / 2), this.posY+ (this.height / 2), this.width, true);
            
            return dd.getCommands();
	    },
	    regist : function(){
	        this._superClass.prototype.regist.call(this);
	        this.bindEvent('mouseup', this._mouseup);
	        this.bindEvent('mousedown', this._mousedown);
	    },
	    _mousedown : function(){
	        this.status = 'active';
	        //this.isActive = false;
	        this.refresh();
	    },
	    _mouseup : function(){
            this.status = 'default';
            //this.isActive = false;
            this.refresh();
        }
	});
	
	
	global && (global.CommonShape = CommonShape);
	return CommonShape;
}));